<template>
  <div>
    <el-card shadow="never">
      <header-title title="菜品统计"></header-title>
    </el-card>
    <el-row>
      <el-col :span="24">
        <el-card shadow="never">
          <div class="small-title">按条件统计</div>
          <el-form ref="form" :model="searchForm" label-width="120px">
            <el-form-item label="统计时间：">
              <el-radio-group v-model="searchForm.dateType">
                <el-radio :label="1">今日</el-radio>
                <el-radio :label="2">昨日</el-radio>
                <el-radio :label="3">近7天</el-radio>
                <el-radio :label="4">近30天</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="统计类别：">
              <el-checkbox-group v-model="topTypeIdArr">
                <el-checkbox :label="item.id" v-for="item in topProductTypeData" :key="item.id">
                  <span>{{item.name}}</span>
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-button @click="search" class="button" perPage="small" round icon="el-icon-search">查询</el-button>
          </el-form>
        </el-card>
        <el-card shadow="never">
          <el-table :data="filterMarketInfoData" v-loading="loading" style="width: 100%">
            <el-table-column prop="productName" label="商品名称"></el-table-column>
            <el-table-column prop="productTypeName" label="类别"></el-table-column>
            <el-table-column label="均价">
              <el-table-column prop="avgPrice" label="当前（元）"></el-table-column>
              <el-table-column prop="prevAvgPrice" label="环比（元）"></el-table-column>
              <el-table-column prop="avgPriceTrend" label="趋势"></el-table-column>
            </el-table-column>
            <el-table-column label="销量">
              <el-table-column prop="totalWeight" label="当前（斤）"></el-table-column>
              <el-table-column prop="prevTotalWeight" label="环比（斤）"></el-table-column>
              <el-table-column prop="totalWeightTrend" label="趋势"></el-table-column>
            </el-table-column>
            <div slot="empty"><img src="../../../assets/blank_tip_page/blank_tip_page.png" alt=""></div>
          </el-table>
          <el-pagination
            :page-size="dishesInfoPage.perPage"
            :current-page="dishesInfoPage.page"
            :total="marketInfoTotal"
            @current-change="changedishesInfoPage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import HeaderTitle from "@/components/HeaderTitle"
import { mapGetters, mapState } from "vuex"
export default {
  name: 'DishesOperating',
  components: {
     HeaderTitle
  },
  data(){
    return {
      searchForm: {
        dateType: 1,
        topTypeId: []
      },
      topTypeIdArr: [],
      dishesInfoPage: {
        page: 1,
        perPage: 10
      },
      loading: true
    }
  },
  watch: {
    '$store.state.app.marketId'(val, oldVal){
      this.getDishesInfoList(
        this.searchForm.dateType,
        this.searchForm.topTypeId,
        this.dishesInfoPage.perPage,
        this.dishesInfoPage.page)
    }
  },
  mounted(){
    this.$store.dispatch("operating/fetchTopProductTypeList")
    this.searchForm.topTypeId = JSON.stringify(this.topTypeIdArr)
    this.searchForm.dateType = this.searchForm.dateType / 1
    this.getDishesInfoList(
      this.searchForm.dateType,
      this.searchForm.topTypeId,
      this.dishesInfoPage.perPage,
      this.dishesInfoPage.page
    )
  },
  computed: {
    ...mapState("operating", [
      "topProductTypeData",
      "marketInfoData",
      "marketInfoTotal"
    ]),
    ...mapGetters("operating", ["filterMarketInfoData"])
  },
  methods:{
    getDishesInfoList(dateType, topTypeId, perPage, page) {
      this.$store.dispatch("operating/fetchDishesInfoData", {
        dateType,
        topTypeId,
        perPage,
        page
      }).then(res => {
        this.loading = false
      })
    },
    changedishesInfoPage(num) {
      this.dishesInfoPage.page = num
      this.searchForm.topTypeId = JSON.stringify(this.topTypeIdArr)
      this.getDishesInfoList(
        this.searchForm.dateType,
        this.searchForm.topTypeId,
        this.dishesInfoPage.perPage,
        num
      )
    },
    search() {
      this.searchForm.topTypeId = JSON.stringify(this.topTypeIdArr)
      this.dishesInfoPage.page = 1
      this.getDishesInfoList(
        this.searchForm.dateType,
        this.searchForm.topTypeId,
        this.dishesInfoPage.perPage,
        this.dishesInfoPage.page
      )
    }
  }
}
</script>

<style lang="scss" scoped>
  /deep/ .el-table__empty-text{
    width: 100%;
  }
.small-title {
  font-size: 16px;
  color: #17233d;
  font-weight: normal;
  margin-bottom: 20px;
}
.button {
  background: #64d9d6;
  color: #fff;
  margin-left: 40px;
}
.el-form-item {
  margin-bottom: 0;
}
</style>

